﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace ASPMVC_first_app.Models
{
    public class Min18YearsForMembershipType: ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var customer = (Customer)validationContext.ObjectInstance;

            if (customer.MembershipTypeId == MembershipType.Unknown || customer.MembershipTypeId == MembershipType.Free)
                return ValidationResult.Success;
            if (customer.DateOfBirth == null)
                return new ValidationResult("Enter your Date of Birth");
            var age = DateTime.Now.Year - customer.DateOfBirth.Value.Year;
            return (age >= 18)
                ?ValidationResult.Success
                : new ValidationResult("Ypu should be at least 18 years old");
        }

    }
}